<?php
include "config.php";
set_time_limit (0);
$address = CLIENT_IP;
$port = CLIENT_PORT;
$sockd;

//connects to SFTPClient process to send a "put files" request
function put($serverPath, $clientPath){
	global $sockd;
	open_socket();	
	
	$message = "PUT:".$serverPath.":".$clientPath.":";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL)) == false){
		echo "<SCRIPT LANGUAGE='javascript'>showErrorMessage(\"Unable to connect:\"".socket_strerror(socket_last_error()).");</SCRIPT>";
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	else{
		//echo "$message \n";
		//echo "sent $bytes bytes from socket_send()";
	}
	
	//blocking for the response (DONE)	
	//echo "Reading response: \n";
	$buf = 'This is my buffer.';
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		else
		{
			//echo "Read $buf from socket_recv(). Closing socket...";
			$result = "DONE";
		}
	}
	close_socket();
	return ($result);
}

function close(){
	global $sockd;
	open_socket();	
	
	$message = "EXI";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL)) == false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	else{
		echo "$message \n";
		echo "sent $bytes bytes from socket_send()";
	}
	
	//blocking for the response (DONE)	
	echo "Reading response: \n";
	$buf = 'This is my buffer.';
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		else
			echo "Read $buf from socket_recv(). Closing socket...";
	}
	close_socket();
}

function disconnect(){
	global $sockd;
	open_socket();	
	
	$message = "DIS";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL)) == false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	else{
		echo "$message \n";
		echo "sent $bytes bytes from socket_send()";
	}
	
	//blocking for the response (DONE)	
	echo "Reading response: \n";
	$buf = 'This is my buffer.';
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		else
			echo "Read $buf from socket_recv(). Closing socket...";
	}
	close_socket();
}

//connects to SFTPClient process to send a "get files" request
function get($serverPath, $clientPath){
	global $sockd;
	open_socket();	
	
	$message = "GET:".$serverPath.":".$clientPath.":";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL))==false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	//else{
		//echo "$message \n";
		//echo "sent $bytes bytes from socket_send()";
	//}
	
	//blocking for the response 
	//echo "Reading response: \n";
	
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		//else
		//	echo "Read $buf from socket_recv(). Closing socket...";
	}

	close_socket();
}

class File{
	public $fileName;
	public $fileType;
	
	 function File($name, $type) {
		$this->fileName = $name;
		$this->fileType = $type;
	}
}


//connects to SFTPClient process to send a "list dir contents" request
function lis($serverPath){
	global $sockd;
	open_socket();
	
/*	class file{
		public $fileName;
		public $fileType;
		
		 function file($name, $type) {
			$this->fileName = $name;
			$this->fileType = $type;
		}
	}*/	
	$listofFiles = array();
	
	$message = "LIS:".$serverPath.":";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL))==false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	//else{
		//echo "$message \n";
		//echo "sent $bytes bytes from socket_send()";
	//}
	
	//blocking for the response (List of file formatted as ":file1:f:file2:f:dir1:d:dir2:d")
	//echo "Reading response: \n";
	$buff = 'this is buffer';

	if (($bytes=socket_recv($sockd, $buff, 2, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		
		$len = ord($buff[0]) * 256; //high order byte
		$len = $len + ord($buff[1]);//add low order byte
		
		if (($bytes=socket_recv($sockd, $buff, $len, MSG_WAITALL)) == false) {
			echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
		}
		else {
			
			//parse buff for a list of files and directories.
			$pieces = explode(":", $buff);
			if($pieces[0]=="FAIL"){
				echo "list dir failed for remote path $serverPath\n";
			}
			else{
				for($i=0; $i<count($pieces)-1; $i=$i+2){
					$listofFiles[] = new File($pieces[$i],$pieces[$i+1]);
				}			
				//echo "received $buff \n";
				//print_r($listofFiles);	
			}		
		}		
	}
	close_socket();
	return ($listofFiles);
}

//connects to SFTPClient process to send a "CONNECTION" request
function connect($ipAddress){
	global $sockd;
	open_socket();
	
	$message = "CON:".$ipAddress.":";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL))== false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	//else{
	//	echo "$message \n";
	//	echo "sent $bytes bytes from socket_send()";
	//}
	
	//blocking for the response (DONE)
	echo "Reading response: \n";
	$buf = 'This is my buffer.';
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		//else
			//echo "Read $buf from socket_recv(). Closing socket...";
	}
	close_socket();	
}

function directConnect($ipAddress,$port){
	global $sockd;
	open_socket();
	
	$message = "DCO:".$ipAddress.":".$port.":";
	
	if(($bytes=socket_send($sockd,$message,strlen($message), MSG_WAITALL))== false){
		die("Unable to connect:" . socket_strerror(socket_last_error()));
	}
	else{
		echo "$message \n";
		echo "sent $bytes bytes from socket_send()";
	}
	
	//blocking for the response (DONE)
	echo "Reading response: \n";
	$buf = 'This is my buffer.';
	if (($bytes=socket_recv($sockd, $buf, 4, MSG_WAITALL)) == false) {
		echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
	} 
	else {
		if($buf !== "DONE")
			echo "Error Sending out Data. Closing socket ....";
		else
			echo "Read $buf from socket_recv(). Closing socket...";
	}
	close_socket();	
}


function open_socket(){
	global $address, $port, $sockd;
	// Create the socket
	if(($sockd = socket_create(AF_INET, SOCK_STREAM,SOL_TCP))<1){
		
		die("Unable to create socket:" . socket_strerror(socket_last_error()));
	}
	else
		//echo "Socket created ...\n";

	if(socket_connect($sockd,$address,$port) == FALSE){
		echo $address;
		echo $port;
		echo "<SCRIPT LANGUAGE='javascript'>showErrorMessage(\"Unable to connect to the client process:\"".socket_strerror(socket_last_error()).");</SCRIPT>";
		die("Unable to connect to the client process:" . socket_strerror(socket_last_error()));
	}
	
	//else
		//echo "Socket connected ...\n";	
}

function close_socket(){
	global $sockd;
	socket_close($sockd);
}

function cut_string_using_last($string){
	$last = substr(strrchr($string, "/"), 1 );
	$filename = substr($last, 0 , strlen($last)-1);
	return ($filename);
}
//connect("127.0.0.1",55555);
//lis("/tmpt/");
//lis("/tmp/");
//get("/tmp/tx.put","/home/sms103/t.xx");
//put("/tmp/putted.x","/tmp/tx.put");
//close();
?>
